package magick;

import magick.util.MagickBitmap;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.magick.R;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

public class AndroidMagickActivity extends Activity {
	static final int PROGRESS_DIALOG = 0;
	ProgressDialog progressDialog = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		try {
			final MagickImage img = new MagickImage(new ImageInfo(
					"/sdcard/3.jpg"));

			Spinner s = (Spinner) findViewById(R.id.spinner);
			ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
					R.array.effects, android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			s.setAdapter(adapter);

			s.setOnItemSelectedListener(new OnItemSelectedListener() {

				public void onItemSelected(AdapterView<?> parent, View view,
						int pos, long id) {
					ProgressThread t = new ProgressThread(img, pos);
					Toast.makeText(getApplicationContext(), "Doing",
							Toast.LENGTH_LONG);
					t.run();
				}

				public void onNothingSelected(AdapterView<?> parent) {
					// Do nothing.
				}
			});
			ImageView iv = (ImageView) findViewById(R.id.imageView);
			iv.setImageBitmap(MagickBitmap.ToBitmap(img));

		} catch (MagickApiException e) {
			e.printStackTrace();
		} catch (MagickException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected Dialog onCreateDialog(int id, Bundle bundle) {
		switch (id) {
		case PROGRESS_DIALOG:
			progressDialog = new ProgressDialog(AndroidMagickActivity.this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.setMessage("Loading...");
			return progressDialog;
		default:
			return null;
		}
	}

	/** Nested class that performs progress calculations (counting) */
	private class ProgressThread extends Thread {

		MagickImage img;
		int pos;

		ProgressThread(MagickImage image, int pos) {
			this.img = image;
			this.pos = pos;
		}

		public void run() {
			int effect = 0;

			switch (pos) {
			case 1:
				effect = NoiseType.UndefinedNoise;
				break;
			case 2:
				effect = NoiseType.UniformNoise;
				break;
			case 3:
				effect = NoiseType.GaussianNoise;
				break;
			case 4:
				effect = NoiseType.MultiplicativeGaussianNoise;
				break;
			case 5:
				effect = NoiseType.ImpulseNoise;
				break;
			case 6:
				effect = NoiseType.LaplacianNoise;
				break;
			case 7:
				effect = NoiseType.PoissonNoise;
				break;
			}
			Bitmap bitmap = null;
			try {
				ImageView iv = (ImageView) findViewById(R.id.imageView);

				MagickImage image = null;
				MagickImage image2 = null;

				if (pos == 10) {
					Log.d("time", "Time1:" + System.currentTimeMillis());
					image = img.averageImages();
					image.levelImageChannel("RedChannel", 0, 245, 1);
					image.modulateImage("150,100,100");
					image.contrastImage(false);
					Bitmap b = Bitmap.createScaledBitmap(MagickBitmap
							.ToBitmap(image), MagickBitmap.ToBitmap(img)
							.getWidth(),
							MagickBitmap.ToBitmap(img).getHeight(), true);
					/*
					 * image2 = img.blurImage(0, 6); Bitmap b2 =
					 * Bitmap.createScaledBitmap( MagickBitmap.ToBitmap(image2),
					 * 612, 612, true); //Bitmap b3 =
					 * BitmapFactory.decodeResource(getResources(),
					 * R.drawable.yellow1); //Bitmap copy =
					 * Bitmap.createScaledBitmap(b3, b2.getWidth(),
					 * b2.getHeight(), true); Log.d("time", "" +
					 * System.currentTimeMillis()); Options options = new
					 * BitmapFactory.Options(); options.inScaled = false;
					 * options.inPreferredConfig = Bitmap.Config.ARGB_8888;
					 * 
					 * Canvas canvas = new Canvas(b2); Paint paint = new
					 * Paint(Paint.FILTER_BITMAP_FLAG);
					 * paint.setAntiAlias(true); paint.setAlpha(255);
					 * canvas.drawBitmap(b, 0, 0, paint);
					 * //canvas.drawBitmap(b3,0 ,0,paint );
					 */
					Log.d("time", "Time1:" + System.currentTimeMillis());

					iv.setImageBitmap(b);
				} else {
					if (pos == 11) {
						Log.d("time", "Time2:" + System.currentTimeMillis());
						Bitmap b = Bitmap.createScaledBitmap(MagickBitmap
								.ToBitmap(img), MagickBitmap.ToBitmap(img)
								.getWidth(), MagickBitmap.ToBitmap(img)
								.getHeight(), true);
						image = img.blurImage(0, 3);
						Bitmap b2 = Bitmap.createScaledBitmap(MagickBitmap
								.ToBitmap(image), MagickBitmap.ToBitmap(img)
								.getWidth(), MagickBitmap.ToBitmap(img)
								.getHeight(), true);

						Canvas canvas = new Canvas(b);
						Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
						paint.setAntiAlias(true);
						paint.setAlpha(170);
						canvas.drawBitmap(b2, 0, 0, paint);
						Log.d("time", "Time2:" + System.currentTimeMillis());
						iv.setImageBitmap(b);
					} else {
						image = img.addNoiseImage(effect);
						if (image != null) {
							bitmap = MagickBitmap.ToBitmap(image);
							iv.setImageBitmap(bitmap);
						}
					}
				}

			} catch (MagickException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

}
